/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaCreditos.java
 *
 * Created on 17-ene-2012, 20:33:29
 */
package invsys.Vistas;

import invsys.Controladores.ControlCliente;
import invsys.Controladores.ControlCredito;
import invsys.Controladores.ControlDetalleCredito;
import invsys.Controladores.ControlDetalleVenta;
import invsys.Controladores.ControlProducto;
import invsys.Controladores.ControlVenta;
import invsys.Modelo.Cliente;
import invsys.Modelo.Credito;
import invsys.Modelo.DetalleCredito;
import invsys.Modelo.DetalleVenta;
import invsys.Modelo.Producto;
import invsys.Modelo.Venta;
import invsys.Utilidades.ManejadorBaseDatos;
import invsys.Utilidades.ValidarorVistas;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author dell
 */
public class VistaCreditos extends javax.swing.JDialog {
 public DefaultTableModel dtm=new DefaultTableModel();
  public DefaultTableModel dtm1=new DefaultTableModel();
  public DefaultTableModel dtm2=new DefaultTableModel();
   public DefaultTableModel dtm3=new DefaultTableModel();
   ValidarorVistas vistas= new ValidarorVistas();
  SimpleDateFormat fechas= new SimpleDateFormat("dd/MM/yyyy");
    /** Creates new form VistaCreditos */
    public VistaCreditos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(this);
        iniciar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Abono = new javax.swing.JDialog();
        PanelDetalles = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        totalabono = new javax.swing.JTextField();
        totalCredito = new javax.swing.JTextField();
        detallescreditos = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        productos = new org.jdesktop.swingx.JXTable(dtm2);
        jScrollPane4 = new javax.swing.JScrollPane();
        abonos = new org.jdesktop.swingx.JXTable(dtm3);
        facturacredito = new javax.swing.JTextField();
        fechacredito = new javax.swing.JTextField();
        saldoact = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        saldocredito1 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        descuento = new javax.swing.JTextField();
        idcliente = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        NombreCliente = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtcredito = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        panelabono = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        nuevafactura = new javax.swing.JTextField();
        abonocr = new javax.swing.JTextField();
        fecha = new com.toedter.calendar.JDateChooser();
        Salir = new javax.swing.JButton();
        abono = new javax.swing.JButton();
        mensaje1 = new javax.swing.JLabel();
        creditos = new javax.swing.ButtonGroup();
        jPanel6 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        ComboCliente = new javax.swing.JComboBox();
        BuscarCliente = new javax.swing.JTextField();
        jPanel3 = new invsys.Imagenes.Fondo.panel("transparente.png");
        jScrollPane3 = new javax.swing.JScrollPane();
        CLientesCredito = new org.jdesktop.swingx.JXTable(dtm);
        jPanel4 = new invsys.Imagenes.Fondo.panel("transparente.png");
        jScrollPane2 = new javax.swing.JScrollPane();
        creditosDetalles = new org.jdesktop.swingx.JXTable(dtm1);
        btAbonar = new javax.swing.JButton();
        jPanel1 = new invsys.Imagenes.Fondo.panel("transparente.png");
        pen = new javax.swing.JRadioButton();
        can = new javax.swing.JRadioButton();
        to = new javax.swing.JRadioButton();
        btAbonar1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        mensaje = new javax.swing.JLabel();

        PanelDetalles.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Del Credito"));

        jLabel5.setText("Factura:");

        jLabel6.setText("Fecha:");

        jPanel9.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        totalabono.setEditable(false);
        totalabono.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 1, 24));
        totalabono.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        totalabono.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Total Abonado", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        totalabono.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                totalabonoActionPerformed(evt);
            }
        });
        totalabono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                totalabonoKeyReleased(evt);
            }
        });

        totalCredito.setEditable(false);
        totalCredito.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 1, 24));
        totalCredito.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        totalCredito.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Total Credito", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        totalCredito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                totalCreditoActionPerformed(evt);
            }
        });
        totalCredito.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                totalCreditoKeyReleased(evt);
            }
        });

        dtm2.addColumn("Codigo");
        dtm2.addColumn("Nombre");
        dtm2.addColumn("Descripcion");
        dtm2.addColumn("Precio");
        dtm2.addColumn("Cantidad");
        dtm2.addColumn("Costo");
        dtm2.addColumn("Cantidad Anterior");
        dtm2.addColumn("Cantidad Restante");
        productos.setEditable(false);
        productos.setEnabled(false);
        jScrollPane1.setViewportView(productos);

        detallescreditos.addTab("Articulos", jScrollPane1);

        dtm3.addColumn("Factura");
        dtm3.addColumn("Fecha");
        dtm3.addColumn("Monto");
        dtm3.addColumn("Saldo");
        jScrollPane4.setViewportView(abonos);

        detallescreditos.addTab("Abonos", jScrollPane4);

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(detallescreditos, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                        .addComponent(totalCredito, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(totalabono, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
                        .addGap(14, 14, 14))))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addComponent(detallescreditos, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(totalCredito, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(totalabono, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                        .addGap(1, 1, 1)))
                .addContainerGap())
        );

        facturacredito.setEditable(false);

        fechacredito.setEditable(false);

        saldoact.setEditable(false);
        saldoact.setFont(new java.awt.Font("Agency FB", 1, 14));
        saldoact.setForeground(new java.awt.Color(51, 102, 0));
        saldoact.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        saldoact.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "SALDO", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 102, 0)));

        jLabel10.setText("Iva:");

        saldocredito1.setEditable(false);

        jLabel11.setText("Descuento:");

        descuento.setEditable(false);

        idcliente.setEditable(false);

        jLabel12.setText("Identificacion:");

        jLabel13.setText("Nombre:");

        NombreCliente.setEditable(false);

        jLabel14.setText("Credito:");

        txtcredito.setEditable(false);

        jLabel15.setText("%");

        javax.swing.GroupLayout PanelDetallesLayout = new javax.swing.GroupLayout(PanelDetalles);
        PanelDetalles.setLayout(PanelDetallesLayout);
        PanelDetallesLayout.setHorizontalGroup(
            PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelDetallesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(PanelDetallesLayout.createSequentialGroup()
                        .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(jLabel13)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(idcliente, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(NombreCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(fechacredito, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                        .addGap(4, 4, 4)
                        .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel10)
                            .addComponent(jLabel14))
                        .addGap(14, 14, 14)
                        .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, PanelDetallesLayout.createSequentialGroup()
                                .addComponent(saldocredito1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(descuento, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtcredito, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(facturacredito, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saldoact, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                .addContainerGap())
        );
        PanelDetallesLayout.setVerticalGroup(
            PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelDetallesLayout.createSequentialGroup()
                .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelDetallesLayout.createSequentialGroup()
                        .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(idcliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(facturacredito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(NombreCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel10)
                                .addComponent(saldocredito1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel11)
                                .addComponent(descuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel13)
                            .addComponent(jLabel15))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtcredito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(PanelDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(fechacredito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel14))
                            .addComponent(jLabel6)))
                    .addComponent(saldoact, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelabono.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Del Abono"));

        jLabel8.setText("Factura:");

        jLabel9.setText("Fecha:");

        nuevafactura.setEditable(false);

        abonocr.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 1, 24));
        abonocr.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        abonocr.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Abono", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        abonocr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abonocrActionPerformed(evt);
            }
        });
        abonocr.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                abonocrKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout panelabonoLayout = new javax.swing.GroupLayout(panelabono);
        panelabono.setLayout(panelabonoLayout);
        panelabonoLayout.setHorizontalGroup(
            panelabonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelabonoLayout.createSequentialGroup()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nuevafactura, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(abonocr, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
        );
        panelabonoLayout.setVerticalGroup(
            panelabonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelabonoLayout.createSequentialGroup()
                .addGroup(panelabonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelabonoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panelabonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(panelabonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel8)
                                .addComponent(nuevafactura)
                                .addComponent(jLabel9)))
                        .addGap(20, 20, 20))
                    .addComponent(abonocr))
                .addContainerGap())
        );

        Salir.setText("SALIR");
        Salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalirActionPerformed(evt);
            }
        });

        abono.setText("ABONAR");
        abono.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abonoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AbonoLayout = new javax.swing.GroupLayout(Abono.getContentPane());
        Abono.getContentPane().setLayout(AbonoLayout);
        AbonoLayout.setHorizontalGroup(
            AbonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AbonoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelabono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AbonoLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(PanelDetalles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AbonoLayout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(mensaje1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(Salir, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(abono, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24))
        );
        AbonoLayout.setVerticalGroup(
            AbonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AbonoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(PanelDetalles, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelabono, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(AbonoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(abono, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Salir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(mensaje1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Creditos", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 18))); // NOI18N

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar Cliente"));

        ComboCliente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nombre:", "Codigo:" }));

        BuscarCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                BuscarClienteKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(ComboCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(BuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(108, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ComboCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Clientes Con Credito"));

        dtm.addColumn("identificacion");
        dtm.addColumn("Nombre Completo");
        dtm.addColumn("Direccion");
        dtm.addColumn("Telefono");
        dtm.addColumn("Celular");
        dtm.addColumn("Saldo");
        CLientesCredito.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CLientesCreditoMouseClicked(evt);
            }
        });
        CLientesCredito.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                CLientesCreditoComponentResized(evt);
            }
        });
        jScrollPane3.setViewportView(CLientesCredito);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 442, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalles Del Creddito"));

        dtm1.addColumn("Facturas");
        dtm1.addColumn("Fechas");
        dtm1.addColumn("Saldos");
        dtm1.addColumn("Estado");
        creditosDetalles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                creditosDetallesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(creditosDetalles);

        btAbonar.setText("Abonar");
        btAbonar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAbonarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Ver Credito"));

        creditos.add(pen);
        pen.setSelected(true);
        pen.setText("Pendientes");
        pen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                penActionPerformed(evt);
            }
        });

        creditos.add(can);
        can.setText("Cancelados");
        can.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                canActionPerformed(evt);
            }
        });

        creditos.add(to);
        to.setText("Todos");
        to.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addComponent(pen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(can)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(to)
                .addContainerGap(66, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(pen)
                .addComponent(can)
                .addComponent(to))
        );

        btAbonar1.setText("Destalles");
        btAbonar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAbonar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btAbonar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btAbonar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(btAbonar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btAbonar1))))
        );

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        mensaje.setText("jLabel1");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(68, 68, 68))
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel6Layout.createSequentialGroup()
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(jPanel6Layout.createSequentialGroup()
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addContainerGap())
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addComponent(mensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(67, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(mensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CLientesCreditoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CLientesCreditoMouseClicked
 int x = CLientesCredito.getSelectedRow();
           if(x < 0){
           mensaje.setText("Selecione primero el producto ");
        }else{ 
        mensaje.setText("");
        String e= dtm.getValueAt(x, 0).toString();
        
        if(pen.isSelected()){
            ListarDetallecreditoPendientes(e);
        }else{ if(can.isSelected()){
            ListarDetallecreditoCancelados(e);
        }else{ if(to.isSelected()){
            ListarDetallecredito(e);
        }else{
        pen.setSelected(true);
        }}}
           }
        // TODO add your handling code here:
    }//GEN-LAST:event_CLientesCreditoMouseClicked

        private void abonocrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abonocrActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_abonocrActionPerformed

    private void abonocrKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_abonocrKeyReleased
vistas.validarnumero(abonocr, mensaje1);
try{double abono1= Double.parseDouble(abonocr.getText());
StringTokenizer sal=new StringTokenizer(saldoact.getText(),"$");
double saldo1= Double.parseDouble(sal.nextToken().trim());
if(abono1>saldo1){
    int saldo2=(int)saldo1;
    abonocr.setText(saldo2+"");
}
}catch(NumberFormatException ex){
    mensaje1.setText("Dato Invalido Diguite Solo Numeros");
}
// TODO add your handling code here:
    }//GEN-LAST:event_abonocrKeyReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void totalabonoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_totalabonoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_totalabonoActionPerformed

    private void totalabonoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_totalabonoKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_totalabonoKeyReleased

    private void totalCreditoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_totalCreditoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_totalCreditoActionPerformed

    private void totalCreditoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_totalCreditoKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_totalCreditoKeyReleased

    private void btAbonarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAbonarActionPerformed
    panelabono.setVisible(true);
abono.setVisible(true);
        avririventana();
        // TODO add your handling code here:
    }//GEN-LAST:event_btAbonarActionPerformed

    private void creditosDetallesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_creditosDetallesMouseClicked
mensaje.setText("");
// TODO add your handling code here:
    }//GEN-LAST:event_creditosDetallesMouseClicked

    private void abonoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abonoActionPerformed
        try {
              if(abonocr.getText().equals("")){               
                  mensaje1.setText("Ingrese El valor Recibido");
                  abonocr.requestFocus();
                  abonocr.selectAll();
                 }else{
            Credito cr=ControlCredito.BuscarCredito(txtcredito.getText());
             credito(cr);}
        } catch (SQLException ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        }
      
    }//GEN-LAST:event_abonoActionPerformed

    private void SalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalirActionPerformed
Abono.dispose();
abonocr.setText("");
        // TODO add your handling code here:
    }//GEN-LAST:event_SalirActionPerformed

    private void CLientesCreditoComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_CLientesCreditoComponentResized

        // TODO add your handling code here:
    }//GEN-LAST:event_CLientesCreditoComponentResized

    private void penActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_penActionPerformed
 int x = CLientesCredito.getSelectedRow();
           if(x < 0){
           mensaje.setText("Selecione primero el producto ");
        }else{ 
        mensaje.setText("");
        String e= dtm.getValueAt(x, 0).toString();
        ListarDetallecreditoPendientes(e);
          btAbonar.setVisible(true); }
        // TODO add your handling code here:
    }//GEN-LAST:event_penActionPerformed

    private void canActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_canActionPerformed
  int x = CLientesCredito.getSelectedRow();
           if(x < 0){
           mensaje.setText("Selecione primero el producto ");
        }else{ 
        mensaje.setText("");
        String e= dtm.getValueAt(x, 0).toString();
        ListarDetallecreditoCancelados(e);
        btAbonar.setVisible(false);
           }
        // TODO add your handling code here:
    }//GEN-LAST:event_canActionPerformed

    private void toActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toActionPerformed
        int x = CLientesCredito.getSelectedRow();
           if(x < 0){
           mensaje.setText("Selecione primero el producto ");
        }else{ 
        mensaje.setText("");
        String e= dtm.getValueAt(x, 0).toString();
        ListarDetallecredito(e);
          btAbonar.setVisible(true);
           }
        // TODO add your handling code here:
    }//GEN-LAST:event_toActionPerformed

    private void BuscarClienteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_BuscarClienteKeyReleased
   if(ComboCliente.getSelectedItem().toString().equals("Codigo:")){
           listarcreditosId(BuscarCliente.getText());
      }else{
          listarcreditosNOM(BuscarCliente.getText());
      }
        // TODO add your handling code here:
    }//GEN-LAST:event_BuscarClienteKeyReleased

    private void btAbonar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAbonar1ActionPerformed
panelabono.setVisible(false);
abono.setVisible(false);
avririventana();
    }//GEN-LAST:event_btAbonar1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
      
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                VistaCreditos dialog = new VistaCreditos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog Abono;
    private javax.swing.JTextField BuscarCliente;
    private org.jdesktop.swingx.JXTable CLientesCredito;
    private javax.swing.JComboBox ComboCliente;
    private javax.swing.JTextField NombreCliente;
    public static javax.swing.JPanel PanelDetalles;
    private javax.swing.JButton Salir;
    private javax.swing.JButton abono;
    private javax.swing.JTextField abonocr;
    private org.jdesktop.swingx.JXTable abonos;
    private javax.swing.JButton btAbonar;
    private javax.swing.JButton btAbonar1;
    private javax.swing.JRadioButton can;
    private javax.swing.ButtonGroup creditos;
    private org.jdesktop.swingx.JXTable creditosDetalles;
    private javax.swing.JTextField descuento;
    public static javax.swing.JTabbedPane detallescreditos;
    private javax.swing.JTextField facturacredito;
    private com.toedter.calendar.JDateChooser fecha;
    private javax.swing.JTextField fechacredito;
    private javax.swing.JTextField idcliente;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel mensaje;
    private javax.swing.JLabel mensaje1;
    private javax.swing.JTextField nuevafactura;
    private javax.swing.JPanel panelabono;
    private javax.swing.JRadioButton pen;
    public static org.jdesktop.swingx.JXTable productos;
    private javax.swing.JTextField saldoact;
    private javax.swing.JTextField saldocredito1;
    private javax.swing.JRadioButton to;
    private javax.swing.JTextField totalCredito;
    private javax.swing.JTextField totalabono;
    private javax.swing.JTextField txtcredito;
    // End of variables declaration//GEN-END:variables

    private void iniciar() {
      listarcreditos();
      dtm1.setNumRows(0);
      
    }

    private void listarcreditos() {
        try {
            dtm.setNumRows(0);
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();      
            ControlCliente.con = mbd.getConexion();
            ControlCliente.listarCliente1("SELECT * FROM clientes WHERE saldo>0");
            Iterator it =  ControlCliente.getCliente().iterator();
                   while (it.hasNext()) {
                               Cliente pro = (Cliente) it.next();
                               int can = (int) pro.getSaldo();
                               String[] fila = {pro.getId(), pro.getNombre()+" "+pro.getApellido(),pro.getDireccion(), pro.getTelefono(),pro.getCelular(),can+""};
                                 dtm.addRow(fila); 
              
                    }
        } catch (SQLException ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
    private void listarcreditosId(String text) {
        try {
            dtm.setNumRows(0);
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();      
            ControlCliente.con = mbd.getConexion();
            ControlCliente.listarCliente1("SELECT * FROM clientes WHERE id LIKE '%"+text+"%' AND saldo>0");
            Iterator it =  ControlCliente.getCliente().iterator();
                   while (it.hasNext()) {
                               Cliente pro = (Cliente) it.next();
                               int can = (int) pro.getSaldo();
                               String[] fila = {pro.getId(), pro.getNombre()+" "+pro.getApellido(),pro.getDireccion(), pro.getTelefono(),pro.getCelular(),can+""};
                                 dtm.addRow(fila); 
              
                    }
        } catch (SQLException ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void listarcreditosNOM(String text) {
        try {
            dtm.setNumRows(0);
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();      
            ControlCliente.con = mbd.getConexion();
            ControlCliente.listarCliente1("SELECT * FROM clientes WHERE nombre LIKE '%"+text+"%' AND saldo>0");
            Iterator it =  ControlCliente.getCliente().iterator();
                   while (it.hasNext()) {
                               Cliente pro = (Cliente) it.next();
                               int can = (int) pro.getSaldo();
                               String[] fila = {pro.getId(), pro.getNombre()+" "+pro.getApellido(),pro.getDireccion(), pro.getTelefono(),pro.getCelular(),can+""};
                                 dtm.addRow(fila); 
              
                    }
        } catch (SQLException ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void ListarDetallecredito(String id) {
             try {
            dtm1.setNumRows(0);
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();      
            ControlCredito.con = mbd.getConexion();
            ControlCredito.listarCredito1("SELECT * FROM creditos WHERE cliente="+id+"");
            Iterator it =  ControlCredito.getCredito().iterator();
                   while (it.hasNext()) {
                               Credito pro = (Credito) it.next();
                               int can = (int) pro.getSaldo();
                               String estado=null;
                               if(can<=0){
                               estado="Cancelado";
                               }else{
                               estado="Pendiente";
                               }
                               String[] fila = {pro.getId(), pro.getFecha(),can+"",estado};
                                 dtm1.addRow(fila); 
              
                    }
        } catch (SQLException ex) {
            Logger.getLogger(VistaCompra.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(VistaCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }
    private void ListarDetallecreditoCancelados(String id) {
             try {
            dtm1.setNumRows(0);
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();      
            ControlCredito.con = mbd.getConexion();
            ControlCredito.listarCredito1("SELECT * FROM creditos WHERE cliente="+id+" AND saldo<=0");
            Iterator it =  ControlCredito.getCredito().iterator();
                   while (it.hasNext()) {
                               Credito pro = (Credito) it.next();
                               int can = (int) pro.getSaldo();
                               String estado=null;
                               if(can<=0){
                               estado="Cancelado";
                               }else{
                               estado="Pendiente";
                               }
                               String[] fila = {pro.getId(), pro.getFecha(),can+"",estado};
                                 dtm1.addRow(fila); 
              
                    }
        } catch (SQLException ex) {
            Logger.getLogger(VistaCompra.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(VistaCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }
    private void ListarDetallecreditoPendientes(String id) {
             try {
            dtm1.setNumRows(0);
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();      
            ControlCredito.con = mbd.getConexion();
            ControlCredito.listarCredito1("SELECT * FROM creditos WHERE cliente="+id+" AND saldo>0");
            Iterator it =  ControlCredito.getCredito().iterator();
                   while (it.hasNext()) {
                               Credito pro = (Credito) it.next();
                               int can = (int) pro.getSaldo();
                               String estado=null;
                               if(can<=0){
                               estado="Cancelado";
                               }else{
                               estado="Pendiente";
                               }
                               String[] fila = {pro.getId(), pro.getFecha(),can+"",estado};
                                 dtm1.addRow(fila); 
              
                    }
        } catch (SQLException ex) {
            Logger.getLogger(VistaCompra.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(VistaCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }
    private void ListarProductosCredito(String credito) {
        
      try {
            dtm2.setNumRows(0);
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();      
            ControlProducto.con = mbd.getConexion();
            ControlProducto.listarProducto1("SELECT p.* FROM detallecredito dc,ventas v,productos p,detalleventas dv WHERE dc.credito='"+credito+"' AND dc.factura=v.num AND v.num=dv.venta AND p.codigo=dv.produto");
            ControlDetalleVenta.listarDetalleVenta1("SELECT dv.* FROM detallecredito dc,ventas v,productos p,detalleventas dv WHERE dc.credito='"+credito+"' AND dc.factura=v.num AND v.num=dv.venta AND p.codigo=dv.produto");
            Iterator it =  ControlProducto.getProducto().iterator();
            Iterator it1 =  ControlDetalleVenta.getDetalleVenta().iterator();
                   while (it.hasNext()&& it1.hasNext()) {
                               DetalleVenta dv=(DetalleVenta) it1.next();
                               Producto pro = (Producto) it.next();
                               int stm = (int) dv.getSubtotal();
                               int can = (int) dv.getCantidad();
                               int cact = (int) dv.getSubtotal();
                               int cant = (int) dv.getCantidad();
                               String[] fila = {pro.getCodigo(),pro.getNombre(),pro.getDescripcion(), pro.getPrecio()+"",can+"",stm+"",cact+"",cant+""};
                                 dtm2.addRow(fila);
              
                    }
        } catch (SQLException ex) {
            Logger.getLogger(VistaCompra.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(VistaCompra.class.getName()).log(Level.SEVERE, null, ex);
        }
                }
    private void Listarabonos(String e) {
             try {
            dtm3.setNumRows(0);
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();      
            ControlDetalleCredito.con = mbd.getConexion();
            ControlDetalleCredito.listarDetalleCredito1("SELECT * FROM detallecredito WHERE credito='"+e+"'");
            Iterator it =  ControlDetalleCredito.getDetalleCredito().iterator();
                   while (it.hasNext()) {
                               DetalleCredito pro = (DetalleCredito) it.next();
                               int mon = (int) pro.getMonto();
                               int can = (int) pro.getSaldo();
                               Venta ve= new Venta();                               
                               String[] fila = {pro.getFactura().getNumero(), pro.getFecha(),"$"+mon+"",can+""};
                                 dtm3.addRow(fila); 
              
                    }
        } catch (SQLException ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void credito(Credito cr) {
             DetalleCredito dcr= new DetalleCredito();
             StringTokenizer s= new StringTokenizer(saldoact.getText(),"$");
             double saldo=Double.parseDouble(s.nextToken());
             double abonoc=Double.parseDouble(abonocr.getText());
               
            try {
                cr.setSaldo(saldo-abonoc);
                cr.setClente(ControlCliente.BuscarClienteid(idcliente.getText()));
                Cliente cli= cr.getClente();
                Venta ve= new Venta();
                ve.setNumero(ControlDetalleCredito.ultimoDetalleCredito(cr.getId()));
                dcr.setFactura(ve);
                dcr.setCredito(cr);
                dcr.setMonto(abonoc);
                dcr.setSaldo(saldo);
                dcr.setFecha(fechas.format(fecha.getDate()));
                dcr.setMovimiento("Abono");
                ControlDetalleCredito.RegistrarDetalleCredito(dcr);
                ControlCredito.ModificarCredito(cr, cr.getId());
                cli.setSaldo(ControlCliente.SaldoCredito(cli.getId()));
                ControlCliente.ModificarCliente(cli,cli.getId());
                
                if (cr.getSaldo()<=0){
                JOptionPane.showMessageDialog(this,"Este Credit Ya esta Cancelado");
                Abono.dispose();
                }else{
                abono.setVisible(true);}
                abonocr.setText("");
                saldoact.setText("$"+cr.getSaldo()+"");
                totalCredito.setText(ControlVenta.BuscarVentaCredito(cr.getId()).getTotal()+"");
                totalabono.setText(ControlDetalleCredito.SaldoCreditoSuma(cr.getId())+"");
                Listarabonos(txtcredito.getText());
                iniciar();
                
                
            } catch (IOException ex) {
                Logger.getLogger(VistaVenta.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(VistaVenta.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(VistaVenta.class.getName()).log(Level.SEVERE, null, ex);
            }
                
                
                      
    
    }

    private void avririventana() {
        int x=creditosDetalles.getSelectedRow();
   int y=CLientesCredito.getSelectedRow();
    if(y <0){
    mensaje.setText("Selecione Primero El Cliente");
    }else
    if(x<0){
    mensaje.setText("Selecione Primero El Credito a Abonar");
    }else{
            try {
        
                String e=dtm1.getValueAt(x,0).toString();
                double sal=Double.parseDouble(dtm1.getValueAt(x,2).toString());
                String Identi=dtm.getValueAt(y,0).toString();
                String Nombre=dtm.getValueAt(y,1).toString();
                Credito cre=ControlCredito.BuscarCredito(e);
                
                facturacredito.setText(ControlVenta.BuscarVentaCredito(e).getNumero());
                fechacredito.setText(cre.getFecha());
                saldoact.setText("$"+cre.getSaldo()+"");
                ListarProductosCredito(e);
                Listarabonos(e);
                totalCredito.setText(ControlVenta.BuscarVentaCredito(e).getTotal()+"");
                totalabono.setText(ControlDetalleCredito.SaldoCreditoSuma(e)+"");
                idcliente.setText(Identi);
                txtcredito.setText(e);
                NombreCliente.setText(Nombre);
                fecha.setDate(new Date());
                nuevafactura.setText(ControlDetalleCredito.ultimoDetalleCredito(e));
                abonocr.requestFocus();
                if (sal<=0){
                JOptionPane.showMessageDialog(this,"Este Credit Ya esta Cancelado");
                abono.setVisible(false);
                abonocr.setVisible(false);
                }
                
                Abono.setModal(true);
                Abono.setResizable(true);
                Abono.setSize(650, 500);
                Abono.setLocationRelativeTo(this);
                Abono.setVisible(true);
                
            }
            catch (SQLException ex) {
                Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
            }            // TODO add your handling code here:
            catch (Exception ex) {
                Logger.getLogger(VistaCreditos.class.getName()).log(Level.SEVERE, null, ex);
            }
    }    
    }

   

   
}
